package com.luke.cloud.order;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import java.util.Collections;

/**
 * @Descrtption BootOrderServiceApp
 * Dubbo Spring Cloud 示例工程：https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/spring-cloud-alibaba-dubbo-examples/README_CN.md
 * @Author luke
 * @Date 2020/4/2
 * dubbo整合Sentinel https://sentinelguard.io/zh-cn/docs/open-source-framework-integrations.html
 **/
@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
public class BootOrderServiceApp {

    public static void main(String[] args) {
        initFlowRule();
        SpringApplication.run(BootOrderServiceApp.class,args);

        /*SpringApplicationBuilder builder = new SpringApplicationBuilder();
        builder.web(WebApplicationType.NONE)
                .sources(BootOrderServiceApp.class).run(args);*/
    }

    /**
     * 流控规则
     */
    private static void initFlowRule(){
        log.info("initFlowRule");
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("com.luke.cloud.user.service.RemoteUserService");//资源
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);//阀值类型
        flowRule.setCount(2);//阀值
        flowRule.setLimitApp("default");
        flowRule.setClusterMode(false);
        flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT);//流控模式
        flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);//流控效果
        FlowRuleManager.loadRules(Collections.singletonList(flowRule));
    }

}
